#!/usr/bin/env python
"""Tophub API 快速演示"""
import asyncio
import os
from datetime import datetime
from tophub_service import TophubService

async def demo():
    """快速演示 Tophub 数据获取"""
    
    os.environ['TOPHUB_API_KEY'] = '1e916be7363ab9ba0042297eb18884d9'
    
    async with TophubService() as service:
        print(f"\n{'='*60}")
        print(f"Tophub API 数据获取演示")
        print(f"时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
        print(f"{'='*60}\n")
        
        # 定义要获取的平台（选择最重要的几个）
        platforms = ['weibo', 'zhihu', 'baidu']
        
        all_data = {}
        
        for platform in platforms:
            print(f"正在获取 {platform} 热榜...")
            data = await service.get_node_data(platform)
            
            if data and 'items' in data:
                items = service.normalize_data(platform, data)
                all_data[platform] = items
                print(f"  ✓ 成功获取 {len(items)} 条数据\n")
                
                # 显示前3条
                for item in items[:3]:
                    print(f"  {item['rank']}. {item['title'][:50]}")
                    if item.get('heat_value'):
                        print(f"     热度: {item['heat_value']}")
                print()
            else:
                print(f"  ✗ 获取失败\n")
                all_data[platform] = []
        
        # 汇总统计
        print(f"\n{'='*60}")
        print("数据汇总")
        print(f"{'='*60}")
        
        total = sum(len(items) for items in all_data.values())
        print(f"\n总计获取 {total} 条热榜数据")
        
        for platform, items in all_data.items():
            print(f"  - {platform}: {len(items)} 条")
        
        # 综合热点 Top 10（按热度排序）
        print(f"\n{'='*60}")
        print("综合热点 Top 10")
        print(f"{'='*60}\n")
        
        all_items = []
        for platform, items in all_data.items():
            all_items.extend(items)
        
        # 排序
        all_items.sort(key=lambda x: x.get('heat_value', 0), reverse=True)
        
        for idx, item in enumerate(all_items[:10], 1):
            print(f"{idx:2}. [{item['platform']:6}] {item['title'][:45]}")
            if item.get('heat_value'):
                print(f"     热度: {item['heat_value']}")
        
        return total > 0


if __name__ == "__main__":
    result = asyncio.run(demo())
    print(f"\n{'='*60}")
    print(f"演示{'成功' if result else '失败'}！")
    print(f"{'='*60}")